package echo.netty.TestProtocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author： zhangnan
 * @date： 2025/5/12 15:54
 * @description： TODO
 * @modifiedBy：
 * @version: 1.0
 */
public class TestMessageCodec {

    public static void main(String[] args) throws Exception {
        // @Sharable 注解含义：该 Handler 是线程安全的，让该 Handler 可以在多线程环境下被共享使用，例如：LoggingHandler
        LoggingHandler loggingHandler = new LoggingHandler();
        EmbeddedChannel channel = new EmbeddedChannel(
                // 解决 粘包半包问题
                new LengthFieldBasedFrameDecoder(1024,12,4,0,0),
                loggingHandler,
                new MessageCodec());

        // 测试 encode
        LoginRequestMessage message = new LoginRequestMessage("zhangsan", "123");
        channel.writeOutbound(message);

        // 测试decode
        // 模拟出发入栈
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, message, buf);



        // 入栈
        channel.writeInbound(buf);

    }

}
